<?php

/**
 * Account API
 *
 * Provides access and CRUD mechanism for accounts.
 *
 * @author Terence Yan <terence.yan@me.com>
 */

require_once 'UTerrorcode.php';
require_once 'AFaccount.controller.php';


/**
 * Read account
 * @param $userName string
 * @param $authUserName string
 * @param $authPasswordHashuser string
 * @return array
 */
function getAccount($username, $authUsername, $authPass) {
    if (empty($username)) {
        return array(ErrorCode::failRead + 100);
    }
    if (empty($authUsername)) {
        return array(ErrorCode::failRead + 200);
    }
    if (empty($authPass)) {
        return array(ErrorCode::failRead + 300);
    }

    $controller = new AccountController();
    if(!($controller -> authenticateValidator($authUsername, $authPass))){
        return array(ErrorCode::authFailRead);
    }
    return $controller ->requestHandler(array(
        ':accName' => $username
        )
    );
}

/**
 * Read all accounts under authorizing user
 * @param $authUsername string
 * @param $authPass string
 * @return array
 */
function getAccounts($authUsername, $authPass) {
    if (empty($authUsername)) {
        return array(ErrorCode::failRead + 100);
    }
    if (empty($authPass)) {
        return array(ErrorCode::failRead + 200);
    }

    $controller = new AccountController();
    if(!($controller -> authenticateValidator($authUsername, $authPass))){
        return array(ErrorCode::authFailRead);
    }
    return $controller ->requestHandler(array()
    );

}

/**
 * Delete Account
 * @param $userName string
 * @param $authUser string
 * @param $authUserPassHash string
 * @param $datahash string
 * @return array
 */
function deleteAccount($username, $authUsername, $authPass, $dataHash) {
    if (empty($username)){
        return array(ErrorCode::failRead + 100);
    }
    if (empty($authUsername)) {
        return array(ErrorCode::failRead + 200);
    }
    if (empty($authPass)) {
        return array(ErrorCode::failRead + 300);
    }
    if (empty($dataHash)) {
        return (ErrorCode::failRead + 400);
    }

    $controller = new AccountController();
    if(!($controller -> authenticateValidator($authUsername, $authPass))){
        return array(ErrorCode::authFailDelete);
    }
    if (!($controller -> managerValidator($authUsername))) {
        return array(ErrorCode::authFailDelete);
    }
    return $controller -> deleteHandler(array(
        ':accName' => $username,
        'dataHash' => $dataHash
    ));
}

/**
 * Create or Update Account
 * NOTE: If Updating account with identical content as existing, update will FAIL.
 * @param $userName string
 * @param $passHash string
 * @param $authUser string
 * @param $authUserHash  string
 * @param $dataHash string
 * @return array
 */
function putAccount($username, $passHash, $authUsername, $authPass, $dataHash) {
    if (empty($username)){
        return array(ErrorCode::failCreate + 100);
    }
    if (empty($passHash)) {
        return array(ErrorCode::failCreate + 200);
    }
    if (empty($authUsername)) {
        return array(ErrorCode::failCreate + 300);
    }
    if (empty($authPass)) {
        return array(ErrorCode::failCreate + 400);
    }

    $controller = new AccountController();
    if(!($controller -> authenticateValidator($authUsername, $authPass))){
        $outcome = ErrorCode::authFailCreate;
    }
    if (!($controller -> managerValidator($authUsername))) {
        $outcome = ErrorCode::authFailCreate;
    }

    $exist = $controller -> requestHandler(array(
        ':accName' => $username));
    if ($exist[0]== ErrorCode::successRead) {
        if (isset($outcome)) {
            return array($outcome + 20);
        }
        return $controller -> actionHandler(array(
            ':accName' => $username,
            ':password' => $passHash,
            'dataHash' => $dataHash
        ));
    } else {
        //create path
        if (isset($outcome)) {
            return array($outcome);
        }
        return $controller -> actionHandler(array(
            ':accName' => $username,
            ':password' => $passHash
        ));
    }
}

/**
 * Set new Employee status value (aka Manager status)
 * @param $username string
 * @param $empStatus string
 * @param $authUsername string
 * @param $authPass string
 * @return array
 */
function setMgrStatus($username, $empStatus, $authUsername, $authPass) {
    if (empty($username)){
        return array(ErrorCode::failUpdate + 100);
    }
    if (empty($empStatus)) {
        return array(ErrorCode::failUpdate + 200);
    }
    if (empty($authUsername)) {
        return array(ErrorCode::failUpdate + 300);
    }
    if (empty($authPass)) {
        return array(ErrorCode::failUpdate + 400);
    }

    $controller = new AccountController();
    if(!($controller -> authenticateValidator($authUsername, $authPass))){
        return array(ErrorCode::authFailUpdate);
    }
    if (!($controller -> managerValidator($authUsername))) {
        return array(ErrorCode::authFailUpdate);
    }
    return $controller -> relatedEntityHandler(array(
        ':accName' => $username,
        ':empStatus' => $empStatus
    ));

}
